//
//  AFViewController.m
//  AFFMDBTools
//
//  Created by xywzxw on 11/01/2022.
//  Copyright (c) 2022 xywzxw. All rights reserved.
//

#import "AFViewController.h"
#import "AFDataBaseTools.h"
#import <AFFMDBTools/ExceptionModel.h>

@interface AFViewController ()

@end

@implementation AFViewController


- (void)viewDidLoad{
    self.view.backgroundColor = [UIColor purpleColor];
    [super viewDidLoad];
    NSArray *arr = @[@"创建数据库",@"存数据",@"取数据",@"取所有数据",@"清空所有数据",@"存储对象",@"查询列表"];
    CGFloat w = 150;
    CGFloat h = 30;
    CGFloat x = (self.view.frame.size.width - w)/2;
    for (int i = 0; i < arr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(x, w + (h+20) * i,w,h);
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        btn.backgroundColor = [UIColor redColor];
        btn.layer.cornerRadius = 5;
        [btn setClipsToBounds:YES];
    }
}
-(void)btnClick:(UIButton *)btn{
    switch (btn.tag) {
        case 100:{
            [AFDataBaseTools configDataBaseWithDbName:@"database.db"];
        }break;
        case 101:{
            [AFDataBaseTools setValue:[NSString stringWithFormat:@"value-%d",arc4random()%10] withKey:[NSString stringWithFormat:@"key-%d",arc4random()%10]];
        }break;
        case 102:{
            NSString *value =  [AFDataBaseTools getConfigValueWithKey:@"key-8"];
            NSLog(@"%@",value);
        }break;
        case 103:{
            NSDictionary *obj = [AFDataBaseTools getAllConfigs];
            for (NSString *key in obj) {
                NSLog(@"%@---%@\n",key,obj[key]);
            }
        }break;
        case 104:{
            [AFDataBaseTools clearAllConfig];
        }break;
        case 105:{
            ExceptionModel *model = [ExceptionModel new];
            model.tableName = @"exception";
            model.systemVersion = @"15.3";
            model.appVersion = @"1.2.0";
            model.appBuildVersion = @"12024011001";
            model.deviceCapabilities = @"deviceCapabilities";
            model.appName = @"anyfit";
            model.bundleId = @"com.anyfit.cs";
            model.platformVersion = @"platformVersion";
            model.env = @"release";
            model.exception = @"exception";
            model.crashTime = @"2024-01-10 09:38:50";
            model.crashTimeStamps = @"1704850722";
            model.loginUser = @"xywzxw";
            
            [AFDataBaseTools insertModel:model];
        }break;
        case 106:{
            ExceptionModel *model = [ExceptionModel new];
            model.tableName = @"exception";
            AFDBQryModel *qry = [AFDBQryModel new];
            qry.page = 1;
            qry.paramStr = @"bundleId like '%any%' and appVersion='1.2.0'";
            qry.rows = 1;

            NSArray *array = [AFDataBaseTools queryListWithModel:model qry:qry];
            NSLog(@"%@",array);
        }break;
        default:
            break;
    }
    
}
@end
